package com.lik888.web1.Warmup;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lik888.web1.entity.GoodsTable;
import com.lik888.web1.entity.nums.Status;
import com.lik888.web1.mapper.GoodsTableMapper;
import com.lik888.web1.service.IGoodsTableService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;


@RequiredArgsConstructor
@Component
public class RedisWarmup {
    private final StringRedisTemplate stringRedisTemplate;
    private final IGoodsTableService goodsTableService;

    @PostConstruct
    //预热商品库存信息
    public boolean init() {
        SetOperations<String, String> stringStringSetOperations = stringRedisTemplate.opsForSet();
        List<GoodsTable> goodsTableList = goodsTableService.list(new LambdaQueryWrapper<GoodsTable>().eq(GoodsTable::getGoodsStatus, Status.NORMAL));
        goodsTableList.stream().map(GoodsTable::getGoodsId).forEach(id -> stringStringSetOperations.add("goods", id.toString()));
       //预热商品库存
        Long size = stringStringSetOperations.size("goods");
        stringRedisTemplate.opsForValue().set("total",size.toString());
        return true;
    }

}
